package com.tools.controller;


import com.tools.common.beans.constant.ControllerConstant;
import com.tools.common.beans.vo.Result;
import com.tools.generator.entity.ProjectModifyEntity;
import com.tools.generator.entity.TableEntity;
import com.tools.generator.entity.TableFieldEntity;
import com.tools.generator.service.TableFieldService;
import com.tools.generator.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 数据表管理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping(ControllerConstant.API_TABLE_PREFIX)
public class TableController extends BaseController {
    @Autowired
    private TableService tableService;

    @Autowired
    private TableFieldService tableFieldService;

    /**
     * 基础路径
     */
    private final String prefix = "generate/";

    @GetMapping(value = {"/{page}.html"})
    public ModelAndView allPage(@PathVariable("page") String page){
        return jumpPage(prefix + page);
    }


    @GetMapping(value = {"/table-edit"})
    public ModelAndView editPage(@RequestParam("id") Long id){
        TableEntity byId = tableService.getById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("table",byId);
        modelAndView.setViewName(prefix + "table-edit");
        return modelAndView;
    }


    /**
     * 分页
     *
     * @param query 查询参数
     */
    @GetMapping("page")
    public Result page(TableEntity query) {
        startPage(query.getPage(), query.getLimit());
        List<TableEntity> page = tableService.page(query);
        return getDataTable(page);
    }


    /**
     * 获取表信息
     *
     * @param id 表ID
     */
    @GetMapping("{id}")
    public Result<TableEntity> get(@PathVariable("id") Long id) {
        TableEntity table = tableService.getById(id);
        // 获取表的字段
        List<TableFieldEntity> fieldList = tableFieldService.getByTableId(table.getId());
        table.setFieldList(fieldList);
        return Result.OK(table);
    }

    /**
     * 修改
     *
     * @param table 表信息
     */
    @PutMapping("update")
    public Result<String> update(@RequestBody TableEntity table) {
        tableService.updateById(table);
        return Result.OK();
    }

    /**
     * 删除
     *
     * @param ids 表id数组
     */
    @DeleteMapping("delete")
    public Result<String> delete(@RequestBody Long[] ids) {
        tableService.deleteBatchIds(ids);
        return Result.OK();
    }

    /**
     * 同步表结构
     *
     * @param id 表ID
     */
    @PostMapping("sync/{id}")
    public Result<String> sync(@PathVariable("id") Long id) {
        tableService.sync(id);
        return Result.OK();
    }

    /**
     * 导入数据源中的表
     * @param datasourceId  数据源ID
     * @param tableNameList 表名列表
     */
    @PostMapping("import/{datasourceId}")
    public Result<String> tableImport(@PathVariable("datasourceId") Long datasourceId, @RequestBody List<String> tableNameList) {
        for (String tableName : tableNameList) {
            tableService.tableImport(datasourceId, tableName);
        }
        return Result.OK();
    }

    /**
     * 修改表字段数据
     *
     * @param tableId        表ID
     * @param tableFieldList 字段列表
     */
    @PutMapping("field/{tableId}")
    public Result<String> updateTableField(@PathVariable("tableId") Long tableId, @RequestBody List<TableFieldEntity> tableFieldList) {
        tableFieldService.updateTableField(tableId, tableFieldList);
        return Result.OK();
    }

}
